package com.daervin.svc.manager;

import com.daervin.svc.common.dto.*;
import com.daervin.svc.common.exception.CommonException;
import com.daervin.svc.common.exception.ErrorCode;
import com.daervin.svc.common.utils.JsonUtil;
import com.daervin.svc.dal.WhCategoryMapper;
import com.daervin.svc.dal.model.WhCategoryDO;
import com.fasterxml.jackson.core.type.TypeReference;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * Created by daervin on 2023/8/18
 */
@Component
public class CategoryManager {
    @Resource
    private WhCategoryMapper whCategoryMapper;
    @Resource
    private SettingManager settingManager;

    public List<CategoryTagMixDTO> getCategoryTags() {
        List<CategoryTagMixDTO> result = new ArrayList<>();
        List<CategoryTagValDTO> tagValDTOList = settingManager.getAll();
        if (CollectionUtils.isEmpty(tagValDTOList)) {
            return result;
        }
        for (CategoryTagValDTO tagValDto : tagValDTOList) {
            CategoryTagMixDTO mixDTO = new CategoryTagMixDTO();
            mixDTO.setTagVal(String.format("%s_%s", tagValDto.getType().getVal(), tagValDto.getVal()));
            mixDTO.setTagTypeDesc(String.format("%s_%s", tagValDto.getType().getDesc(), tagValDto.getValDesc()));
            result.add(mixDTO);
        }
        return result;
    }

    public List<WhCategoryDTO> getCategory() {
        List<WhCategoryDO> categoryDOList = whCategoryMapper.selectAll();
        if (CollectionUtils.isEmpty(categoryDOList)) {
            return Collections.emptyList();
        }
        Map<String, CategoryTagTypeDTO> tagTypeMap = settingManager.getTagTypeMap();

        return categoryDOList.stream().map(it -> {
            WhCategoryDTO categoryDTO = new WhCategoryDTO();
            BeanUtils.copyProperties(it, categoryDTO);
            if (!StringUtils.isEmpty(it.getExt())) {
                List<WhCategoryTagDTO> tagList = JsonUtil.toObject(it.getExt(), new TypeReference<List<WhCategoryTagDTO>>() {
                });
                List<String> reqTags = new ArrayList<>();
                tagList.stream().forEach(tag -> {
                    CategoryTagTypeDTO tagTypeDTO = tagTypeMap.get(tag.getTagType());
                    tag.setTagTypeDesc(tagTypeDTO.getDesc());
                    reqTags.add(String.format("%s_%s", tagTypeDTO.getVal(), tag.getTagVal()));
                });
                categoryDTO.setTagList(tagList);
                categoryDTO.setReqTags(reqTags);
            }
            return categoryDTO;
        }).collect(Collectors.toList());
    }

    public Map<Long, WhCategoryDTO> getCategoryMap() {
        List<WhCategoryDTO> list = getCategory();
        if (CollectionUtils.isEmpty(list)) {
            return Collections.emptyMap();
        }
        return list.stream().collect(Collectors.toMap(WhCategoryDTO::getId, Function.identity(), (k1, k2) -> k2));
    }

    public WhCategoryDTO match(Long id) {
        List<WhCategoryDTO> list = getCategory();
        if (CollectionUtils.isEmpty(list)) {
            return null;
        }
        return list.stream().filter(it -> it.getId().compareTo(id) == 0).findFirst().orElse(null);
    }

    public boolean addOrUpdate(WhCategoryDTO categoryDTO) {
        if (!CollectionUtils.isEmpty(categoryDTO.getReqTags())) {
            List<WhCategoryTagDTO> tagList = new ArrayList<>();
            for (String reqTagStr : categoryDTO.getReqTags()) {
                String[] strArr = reqTagStr.split("_");
                CategoryTagValDTO tagValDTO = settingManager.match(strArr[0], strArr[1]);
                if (tagValDTO == null) {
                    throw new CommonException(ErrorCode.ERROR_PARAM, "品类标签存在不合法");
                }
                WhCategoryTagDTO tagDTO = new WhCategoryTagDTO();
                tagDTO.setTagType(tagValDTO.getType().getVal());
                tagDTO.setTagVal(tagValDTO.getVal());
                tagDTO.setTagTypeDesc(tagValDTO.getType().getDesc());
                tagList.add(tagDTO);
            }
            categoryDTO.setTagList(tagList);
        }

        WhCategoryDO record = new WhCategoryDO();
        record.setId(categoryDTO.getId());
        record.setName(categoryDTO.getName());
        if (!StringUtils.isEmpty(categoryDTO.getTagList())) {
            record.setExt(JsonUtil.toString(categoryDTO.getTagList()));
        }
        record.setUpdateTime(new Date());
        if (categoryDTO.getId() != null && categoryDTO.getId() > 0) {
            whCategoryMapper.updateByPrimaryKeySelective(record);
            return true;
        }
        record.setCreateTime(new Date());
        whCategoryMapper.insertSelective(record);
        return true;
    }
}
